/* eslint-disable no-extend-native */
/**
 * 缓存应用常用 的业务数据
 * @param  {[type]} Vue [description]
 * @return {[type]}     [description]
 */
export default (Vue) => {
  var _this = Vue.prototype

  const month = 30 * 24 * 60 * 60 * 1000
  const day = 24 * 60 * 60 * 1000
  const hour = 60 * 60 * 1000
  const minute = 60 * 1000

  const GB = (1024 * 1024 * 1024)
  const MB = (1024 * 1024)
  const KB = (1024)

  /**
   * 四舍五入
   * @param  {[type]} input [description]
   * @param  {[type]} point [description]
   * @return {[type]}       [description]
   */
  Vue.prototype.$scale = function (input, point) {
    var tmp = 0.5
    for (var i = 0; i < point; i++) {
      tmp = tmp / 10
    }

    input += tmp

    var str = input + ''

    var index = str.indexOf('.')

    return str.substring(0, index) + str.substring(index, index + point + 1)
  }

  /**
   * 文件大小转义显示
   * @param  {[type]} bytes [description]
   * @return {[type]}       [description]
   */
  Vue.prototype.$getFileSize = function (bytes) {
    if (!bytes) {
      return ''
    }

    if (bytes >= GB) {
      let result = bytes / GB
      return _this.$scale(result, 2) + ' gb'
    }

    if (bytes >= MB) {
      let result = bytes / MB
      return _this.$scale(result, 2) + ' mb'
    }

    if (bytes >= KB) {
      let result = bytes / KB
      return _this.$scale(result, 2) + ' kb'
    }

    return bytes + ' byte'
  }


  /**
   * 将文本内容拷贝至剪切板
   * @param  {[type]} content [文本内容]
   * @param  {[type]} msg     [复制成功后的提示]
   */
  Vue.prototype.$copyToClip = function (content, msg) {
    var aux = document.createElement('input')
    // aux.setAttribute("style", 'display: none;');
    aux.setAttribute('value', content)
    document.body.appendChild(aux)
    aux.select()
    document.execCommand('copy')
    document.body.removeChild(aux)

    if (msg) {
      _this.$message({
        type: 'success',
        message: msg
      })
    }
  }



  // 将挂在原型链上的方法设置为不可被枚举，不设置的话会跟一些严格检测的包冲突
  Object.defineProperty(Array.prototype, 'contains', {
    writable: true,
    enumerable: false,
    configurable: true,
    value: function (value) {
      if (this.length == 0) {
        return null
      }

      for (var i = 0, len = this.length; i < len; i++) {
        if (this[i] == value) {
          return true
        }
      }
      return false
    }
  })

  Object.defineProperty(Array.prototype, 'containsAny', {
    writable: true,
    enumerable: false,
    configurable: true,
    value: function (values) {
      if (this.length == 0 || values.length == 0) {
        return false
      }

      for (var i = 0, len = this.length; i < len; i++) {
        for (var k = 0, len2 = values.length; k < len; k++) {
          if (this[i] == values[k]) {
            return true
          }
        }
      }
      return false
    }
  })

}
